package com.wsc.wisdomchinaqinghe.activitity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.constant.Constant;
import com.wsc.wisdomchinaqinghe.javaBean.AddressListBean;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.MyToast;


/*
    地址修改
 */

public class ChangeAddressAvtivity extends BaseActivity implements View.OnClickListener,ContributorView {
    private ImageView regist_back,iv_select;
    private TextView tv_submit,tv_headtitle,tv_district;
    private MyToast myToast;
    private Context context;
    private ContributorPresenter contributorPresenter;
    private final String MY_MODULE_UPDATE_ADDRESS_FLAG="MY_MODULE_UPDATE_ADDRESS_FLAG";
    private final String MY_MODULE_UPDATE_ADDRESS_UPDATEFLAG="MY_MODULE_UPDATE_ADDRESS_UPDATEFLAG";
    private String title;
    private EditText tv_name;
    private EditText tv_phone;
    private EditText tv_detailaddress;
    private boolean isClick;
    private RelativeLayout layout_address;
    private AddressListBean addressListBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.address_change_layout);
        context=this;
        myToast=new MyToast(context);
        title=getIntent().getStringExtra("headtitle");
        if(title==null)
            title="添加地址";
        String objectString=getIntent().getStringExtra("object");
        if(objectString!=null)
            addressListBean=JsonTools.jsonStringToObject(objectString,AddressListBean.class);
        initView();
        contributorPresenter = new ContributorPresenter(this, context);
    }

    private void initView()
    {
        regist_back=(ImageView) findViewById(R.id.regist_back);
        tv_submit=(TextView) findViewById(R.id.tv_submit);
        regist_back.setOnClickListener(this);
        tv_submit.setOnClickListener(this);
        tv_submit.setText("提交");
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText(title);
        iv_select=(ImageView) findViewById(R.id.iv_select);
        tv_name=(EditText) findViewById(R.id.tv_name);
        tv_phone=(EditText) findViewById(R.id.tv_phone);
        tv_detailaddress=(EditText) findViewById(R.id.tv_detailaddress);
        iv_select.setOnClickListener(this);
        layout_address=(RelativeLayout) findViewById(R.id.layout_address);
        layout_address.setOnClickListener(this);
        tv_district=(TextView) findViewById(R.id.tv_district);
        if(addressListBean!=null)
        {
            tv_name.setText(addressListBean.getName());
            tv_phone.setText(addressListBean.getPhone());
            if(addressListBean.getType()==0)
            {
                iv_select.setImageResource(R.mipmap.c2);
                isClick=false;
            }else
            {
                iv_select.setImageResource(R.mipmap.c1);
                isClick=true;
            }
            tv_district.setText(addressListBean.getAddressHead());
            tv_detailaddress.setText(addressListBean.getAddressFoot());
        }
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            //发送验证码
            if (flag.equals(MY_MODULE_UPDATE_ADDRESS_FLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                if(code.equals("10700"))
                {
                    myToast.showShort("操作失败");
                }else if(code.equals("200"))
                {
                    setResult(1);
                    finish();
                }
            }else if(flag.equals(MY_MODULE_UPDATE_ADDRESS_UPDATEFLAG))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                if(code.equals("10700"))
                {
                    myToast.showShort("操作失败");
                }else if(code.equals("200"))
                {
                    setResult(1);
                    finish();
                }
            }

        } catch (Exception e) {

        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error,String flag) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.regist_back:
                finish();
                break;
            case R.id.tv_submit:
                String name=tv_name.getText().toString().trim();
                String phone=tv_phone.getText().toString().trim();
                String district=tv_district.getText().toString().trim();
                String detailstring=tv_detailaddress.getText().toString().trim();
                int type=0;
                if(isClick)
                    type=1;
                else
                    type=0;
                if(name.isEmpty()|| phone.isEmpty()|| district.isEmpty()|| detailstring.isEmpty())
                {
                    myToast.showShort("请填写完整");
                }else
                {
                    String uid= Tools.getSharedPreferencesStrings(Constant.SharePreferenceUserid,"",context);
                    if(addressListBean!=null)
                    {
                        //update
                        contributorPresenter.getAddrUpdate(uid,addressListBean.getId(),name,
                                phone,district,detailstring,type,MY_MODULE_UPDATE_ADDRESS_UPDATEFLAG);
                    }else {
                        //add
                        contributorPresenter.getAddAddress(uid,name,phone,district,detailstring,type,MY_MODULE_UPDATE_ADDRESS_FLAG);
                    }

                }
                break;
            case R.id.iv_select:
                isClick=!isClick;
                if(isClick)
                {
                    iv_select.setImageResource(R.mipmap.c1);
                }else {
                    iv_select.setImageResource(R.mipmap.c2);
                }
                break;
            case R.id.layout_address:
                Intent intent=new Intent(context,ProvinceInfoActivity.class);
                startActivityForResult(intent,0);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==0 && resultCode==1)
        {
            String addr=data.getStringExtra("addr");
            tv_district.setText(addr);
        }
    }
}
